﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuadRemote.Core;
using System.Drawing;
using System.Windows.Forms;

namespace QuadRemote.Panels
{
    class BTPanel : Graph
    {
        private GraphData incoming;
        private GraphData outgoing;
        private GraphData resent;

        private long lastUpdate;

        public BTPanel()
            : base("Wireless", 1, 50, Color.Yellow)
        {
            incoming = new GraphData();
            incoming.display = GraphDisplay.REALTIME;
            incoming.name = "Incoming";
            incoming.scale = 1;
            incoming.type = GraphType.DIFFERENTIAL_UPSCALED;
            incoming.unit = GraphUnit.BYTE;
            incoming.pen = Pens.Pink;

            outgoing = new GraphData();
            outgoing.display = GraphDisplay.REALTIME;
            outgoing.name = "Outgoing";
            outgoing.scale = 1;
            outgoing.type = GraphType.DIFFERENTIAL_UPSCALED;
            outgoing.unit = GraphUnit.BYTE;
            outgoing.pen = Pens.Green;


            resent = new GraphData();
            resent.display = GraphDisplay.REALTIME;
            resent.name = "Missed";
            resent.scale = 1;
            resent.type = GraphType.DIFFERENTIAL_UPSCALED;
            resent.unit = GraphUnit.BYTE;
            resent.pen = Pens.DarkOrange;

            lastUpdate = DateTime.Now.Ticks;

            graphs.Add(incoming);
            graphs.Add(outgoing);
            graphs.Add(resent);


            Button reset = new Button();
            reset.Text = "Reset";
            reset.Left = 70;
            reset.Top = 10;
            reset.Width = 50;
            reset.Click += new EventHandler(reset_click);

            drawingPanel.Controls.Add(reset);

            init();
        }

        public void reset_click(object sender, EventArgs args)
        {
            Bluetooth.instance().closePort();
        }

        public override void PaintPanel(System.Drawing.Graphics g)
        {
            long now = DateTime.Now.Ticks;
            //signal.Text = Math.Round((Bluetooth.instance().signalStrength / 2.55),1) + "%";

            if ((now - lastUpdate) * _freq >= 10000000)
            {
                lastUpdate = now;
                bufferOffset++;
                if (bufferOffset == bufferSize) bufferOffset = 0;

                incoming.data[bufferOffset] = Bluetooth.instance().bytesReceived;
                outgoing.data[bufferOffset] = Bluetooth.instance().bytesSent;
                resent.data[bufferOffset] = Bluetooth.instance().bytesResent;
            }

            base.PaintPanel(g);
        }
    }
}
